<template>
  <div class="heatmap-chart">
    <div class="chart-header">
      <h3>瓷砖压力分布热图</h3>
      <div class="chart-controls">
        <button class="control-btn" @click="$emit('area-filter')">区域</button>
        <button class="control-btn" @click="$emit('filter')">筛选</button>
        <button class="control-btn" @click="$emit('reset-view')">重置视图</button>
      </div>
    </div>
    
    <div class="heatmap-container">
      <div class="heatmap-tooltip" v-if="hoveredCell">
        ID: {{ hoveredCell.id }} | 位置: {{ hoveredCell.position }} | 压力值: {{ hoveredCell.pressure }} kPa
      </div>
      
      <div class="heatmap-grid">
        <div v-for="(row, rowIndex) in heatmapData" :key="rowIndex" class="heatmap-row">
          <div 
            v-for="(cell, colIndex) in row" 
            :key="colIndex"
            class="heatmap-cell"
            :class="getPressureLevel(cell.pressure)"
            @mouseenter="hoveredCell = cell"
            @mouseleave="hoveredCell = null"
            @click="$emit('cell-click', cell)"
          ></div>
        </div>
      </div>
      
      <div class="heatmap-legend">
        <span class="legend-label">压力值分布 (kPa)</span>
        <div class="legend-gradient"></div>
        <div class="legend-values">
          <span>0</span>
          <span>3.5</span>
          <span>7.0</span>
        </div>
      </div>
      
      <!-- 视图控制按钮 -->
      <div class="view-controls">
        <button class="view-btn" @click="$emit('toggle-2d')">2D视图</button>
        <button class="view-btn" @click="$emit('toggle-zoom')">缩放视图</button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'HeatmapChart',
  props: {
    heatmapData: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      hoveredCell: null
    }
  },
  methods: {
    getPressureLevel(pressure) {
      const value = parseFloat(pressure);
      if (value < 2) return 'pressure-low';
      if (value < 4) return 'pressure-medium';
      if (value < 6) return 'pressure-high';
      return 'pressure-critical';
    }
  },
  emits: ['area-filter', 'filter', 'reset-view', 'cell-click', 'toggle-2d', 'toggle-zoom']
}
</script>

<style scoped>
.heatmap-chart {
  background: #FFFFFF;
  border: 1px solid #F0F0F0;
  border-radius: 8px;
  overflow: hidden;
}

.chart-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 24px;
  border-bottom: 1px solid #F0F0F0;
}

.chart-header h3 {
  font-size: 16px;
  font-weight: 600;
  color: rgba(0, 0, 0, 0.88);
  margin: 0;
}

.chart-controls {
  display: flex;
  gap: 8px;
}

.control-btn {
  background: #FFFFFF;
  border: 1px solid #D9D9D9;
  border-radius: 6px;
  padding: 5px 16px;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.88);
  cursor: pointer;
  box-shadow: 0px 2px 0px 0px rgba(0, 0, 0, 0.02);
}

.control-btn:hover {
  background: #f5f5f5;
}

.heatmap-container {
  padding: 24px;
  position: relative;
}

.heatmap-tooltip {
  color: #FFFFFF;
  font-size: 12px;
  margin-bottom: 12px;
  background: rgba(0, 0, 0, 0.7);
  padding: 8px 12px;
  border-radius: 4px;
  display: inline-block;
  min-height: 15px;
}

.heatmap-grid {
  display: grid;
  grid-template-columns: repeat(10, 1fr);
  gap: 2px;
  margin-left: 103px;
  margin-bottom: 20px;
}

.heatmap-cell {
  width: 64px;
  height: 22px;
  border-radius: 4px;
  cursor: pointer;
  transition: transform 0.2s ease;
}

.heatmap-cell:hover {
  transform: scale(1.1);
  z-index: 10;
  position: relative;
}

.pressure-low { background: #52C41A; }
.pressure-medium { background: #95DE64; }
.pressure-high { background: #FAAD14; }
.pressure-critical { background: #FF4D4F; }

.heatmap-legend {
  position: absolute;
  bottom: 24px;
  right: 24px;
  background: rgba(255, 255, 255, 0.9);
  padding: 10px;
  border-radius: 4px;
  box-shadow: 0px 2px 8px rgba(0, 0, 0, 0.1);
}

.legend-label {
  font-size: 12px;
  color: rgba(0, 0, 0, 0.75);
  display: block;
  margin-bottom: 5px;
}

.legend-gradient {
  width: 150px;
  height: 10px;
  background: linear-gradient(90deg, #52C41A 0%, #FAAD14 50%, #FF4D4F 100%);
  border-radius: 2px;
  margin-bottom: 5px;
}

.legend-values {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: rgba(0, 0, 0, 0.65);
}

.view-controls {
  position: absolute;
  top: 24px;
  right: 24px;
  display: flex;
  gap: 10px;
}

.view-btn {
  background: #FFFFFF;
  border: 1px solid #D9D9D9;
  border-radius: 6px;
  padding: 5px 15px;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.88);
  cursor: pointer;
  box-shadow: 0px 2px 0px 0px rgba(0, 0, 0, 0.02);
}

.view-btn:hover {
  background: #f5f5f5;
}
</style>
